<template>
  <div class="page">
    <LsdDialog :dialogConfig="dialogConfig" @handleBtn="handleBtn" @close="handleClose" class="gdsp">
      <LsdDialogTitle slot="dialog" :titleConfig="titleConfig" :activeNames="activeNames">
        <DialogForm ref="formComponent" slot="slot1" :formConfig="formConfig" />
        <DialogForm ref="formComponent1" slot="slot2" :formConfig="formConfig1" />
        <el-form slot="slot2" ref="form" :model="upload_form" label-width="140px" size="small" class="upload">
          <el-form-item label="附件：">
            <el-upload class="upload-demo" action="" :file-list="upload_form.fileList" ref="upload"
              :on-preview="handlePreview" disabled>
            </el-upload>
          </el-form-item>
        </el-form>
        <DialogForm ref="formComponent2" slot="slot3" :formConfig="formConfig2" />
        <DialogForm ref="formComponent3" slot="slot4" :formConfig="formConfig3" />
      </LsdDialogTitle>
    </LsdDialog>
  </div>
</template>

<script>
import {
  getinfo,
  khfwFeedbackapprove
} from "@/api/EmergencyDisposal/khfw";
import { showTime } from "@/lib";
import { loadFile } from "@/api/common.js";
export default {
  name: "",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: function () {
        return "";
      }
    }
  },
  data() {
    return {
      // 弹窗表单信息
      formConfig3: {
        formData: [
          {
            type: "input",
            label: "审批意见",
            prop: "approvalOpinion",
            class: "sjgy",
            lableWidth: "140px"
          },
          {
            type: "input",
            label: "审批人",
            prop: "approvalUser",
            // class: "third",
            isDisabled: true,
            lableWidth: "140px"
          },
          {
            type: "input",
            label: "单位及部门",
            prop: "approvalDept",
            // class: "third",
            isDisabled: true,
            lableWidth: "110px"
          },
          {
            type: "input",
            label: "审批时间",
            prop: "approvalTime",
            class: "third",
            isDisabled: true,
            lableWidth: "140px"
          }
        ],
        rules: {
        }
      },
      // 弹窗表单信息
      formConfig2: {
        formData: [
          {
            type: "input",
            label: "填报人",
            prop: "creator",
            // class: "third",
            lableWidth: "140px",
            isDisabled: true
          },
          {
            type: "input",
            label: "单位及部门",
            prop: "creatorUnit",
            // class: "third",
            lableWidth: "110px",
            isDisabled: true
          },
          {
            type: "input",
            label: "填报时间",
            prop: "createTime",
            class: "third",
            lableWidth: "140px",
            isDisabled: true
          }
        ],
        rules: {
          sourceInfoId: [
            { required: true, message: "请选择响应命令单", trigger: "change" }
          ]
        }
      },
      // 弹窗表单信息
      formConfig1: {
        formData: [
          {
            type: "input",
            label: "工单名称",
            prop: "orderName",
            class: "half",
            lableWidth: "140px",
            isDisabled: true
          },
          {
            type: "select",
            label: "工单类型",
            prop: "taskType",
            dic: "dic_gdlx",
            class: "half",
            lableWidth: "140px",
            isShow: true,
            option: [
            ],
            isDisabled: true,
            name: "dutyUnit"
          },
          {
            type: "input",
            label: "派发单位/部门",
            prop: "creatorUnit",
            class: "half",
            lableWidth: "140px",
            isDisabled: true
          },
          {
            type: "dataPicker",
            label: "要求完成时间",
            prop: "planFinishTime",
            class: "half",
            dateType: "datetime",
            lableWidth: "140px",
            isDisabled: true
          },
          {
            type: "select",
            label: "责任单位/部门",
            prop: "dutyUnit",
            dic: "dic_zrdwbm",
            class: "half",
            lableWidth: "140px",
            isShow: true,
            option: [],
            isDisabled: true,
            name: "dutyUnit"
          },
          {
            type: "select",
            label: "责任人",
            prop: "dutyOfficerId",
            dic: "dic_zrr",
            class: "half",
            lableWidth: "140px",
            isShow: true,
            option: [
            ],
            isDisabled: true,
            name: "dutyOfficer"
          },
          {
            type: "select",
            label: "用户类型",
            prop: "userType",
            dic: "dic_yhlx",
            class: "half",
            lableWidth: "140px",
            isShow: true,
            option: [],
            isDisabled: true,
            name: "userType"
          },
          {
            type: "input",
            label: "故障地址",
            prop: "breakdownAddress",
            class: "half",
            lableWidth: "140px",
            isDisabled: true
          },
          {
            type: "textarea",
            label: "任务内容",
            prop: "orderContent",
            class: "sjgy",
            lableWidth: "140px",
            isDisabled: true
          }
        ],
        rules: {
          orderName: [
            { required: true, message: "请输入工单名称", trigger: "blur" }
          ],
          gdlx: [
            { required: true, message: "请选择工单类型", trigger: "change" }
          ],
          creatorUnit: [
            { required: true, message: "请输入派发单位/部门", trigger: "blur" }
          ],
          planFinishTime: [
            { required: true, message: "请选择要求完成时间", trigger: "change" }
          ],
          dutyUnit: [
            { required: true, message: "请选择责任单位/部门", trigger: "change" }
          ],
          userType: [
            { required: true, message: "请选择用户类型", trigger: "change" }
          ],
          breakdownAddress: [
            { required: true, message: "请输入故障地址", trigger: "blur" }
          ],
          orderContent: [
            { required: true, message: "请输入任务内容", trigger: "blur" }
          ]
        }
      },
      // 弹窗表单信息
      formConfig: {
        formData: [
          {
            type: "select",
            label: "响应命令单",
            prop: "sourceInfoId",
            name: "sourceInfo",
            dic: "dic_xymld",
            class: "sjgy",
            lableWidth: "140px",
            isShow: true,
            option: [],
            isDisabled: true
          }
        ],
        rules: {
          sourceInfoId: [
            { required: true, message: "请选择响应命令单", trigger: "change" }
          ]
        }
      },
      // 折叠面板信息
      titleConfig: [
        {
          title: "信息来源",
          name: "1",
          slot: "slot1"
        },
        {
          title: "任务信息",
          name: "2",
          slot: "slot2"
        },
        {
          title: "填报信息",
          name: "3",
          slot: "slot3"
        },
        {
          title: "审批信息",
          name: "4",
          slot: "slot4"
        }
      ],
      // 弹窗信息
      dialogConfig: {
        title: "客户抢修工单",
        isShow: false, // 弹窗显示隐藏
        btnList: [
          {
            name: "同意",
            id: "agree", // 事件名
            type: "primary",
            size: "small"
          },
          {
            name: "驳回",
            id: "reject",
            size: "small"
          },
          {
            name: "取消",
            id: "cancel",
            size: "small"
          }
        ]
      },
      activeNames: ["1", "2", "3", "4"],
      upload_form: {
        fileList: []
      }
    };
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.getinfo();
          this.dialogConfig.isShow = true;
          this.$nextTick(() => {
            // const { roleCode } = this.getCreator.role;
            // if (roleCode == "hb_spr") {
            this.$set(this.$refs.formComponent3.form, "approvalUser", this.getCreator.name);
            this.$set(this.$refs.formComponent3.form, "approvalDept", sessionStorage.getItem("dwbmName"));
            // this.$set(this.$refs.formComponent3.form, "approvalTime", new Date().toLocaleString())
            this.$set(this.$refs.formComponent3.form, "approvalTime", showTime(new Date()));
            // }
          });
        } else {
          if (this.dialogConfig.isShow) {
            this.dialogConfig.isShow = false;
            this.$emit("list");
            this.cancel();
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    getCreator() {
      const account = JSON.parse(sessionStorage.getItem("account")).realName;
      const org = sessionStorage.getItem("dwbmName");
      const creator = {
        name: account,
        org: org
      };
      return creator;
    }
  },
  methods: {
    async getinfo() {
      const res = await getinfo({ id: this.id });
      this.$set(this.$refs.formComponent, "form", res);
      this.$set(this.$refs.formComponent1, "form", res);
      this.$set(this.$refs.formComponent2, "form", res);
      // this.$set(this.$refs.formComponent3, "form", res)
      this.upload_form.fileList = (res.attachInfo && res.attachInfo.map(item => {
        return {
          name: item.attachName,
          url: item.attachId,
          ...item
        };
      })) || [];
    },
    handleClose() {
      this.cancel();
    },
    handleBtn(item) {
      this[item.id]();
    },
    cancel() {
      this.$refs.formComponent3.form = this.$refs.formComponent3.$options.data().form;
      this.upload_form.fileList = [];
      this.$nextTick(() => {
        this.$refs.formComponent3.$refs.formInlines.clearValidate();
      });
      this.$emit("update:visible", false);
    },
    async agree() {
      this.$refs.formComponent3.isValidate();
      const flag = this.$refs.formComponent3.flag;
      if (flag) {
        console.log(this.$refs.formComponent3.form);
        const approvalOpinion = this.$refs.formComponent3.form.approvalOpinion || "";
        await khfwFeedbackapprove({ id: this.id, approvalOpinion: approvalOpinion, approveSign: "pass" });
        this.$message({
          message: "审核成功",
          type: "success"
        });
        this.$emit("update:visible", false);
      }

    },
    async reject() {
      this.$refs.formComponent3.isValidate();
      const flag = this.$refs.formComponent3.flag;
      console.log(this.$refs.formComponent3.form);
      if (flag) {
        const approvalOpinion = this.$refs.formComponent3.form.approvalOpinion || "";
        await khfwFeedbackapprove({ id: this.id, approvalOpinion: approvalOpinion, approveSign: "reject" });
        this.$message({
          message: "驳回成功",
          type: "success"
        });
        this.$emit("update:visible", false);
      }
    },
    // 下载
    handlePreview(file) {
      console.log(file, "file");
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.6)"
      });
      loadFile({ id: file.attachId }).then(res => {
        const a = document.createElement("a");
        const objectUrl = URL.createObjectURL(res);
        a.setAttribute("href", objectUrl);
        a.setAttribute("download", file.name);
        a.click();
        loading.close();
      }).catch(() => {
        loading.close();
      });
    }
  }
};
</script>

<style scoped lang="less">
.upload {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}

:deep(.el-upload) {
  display: none;
}

:deep(.is-disabled) {
  .el-upload-list__item:first-child {
    margin-top: 4px;
  }
}

.gdsp {
  :deep(.el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label:before) {
    display: none;
  }

  :deep(.input_readonly .el-textarea__inner) {
    min-height: auto !important;
    height: auto !important;
  }
}

.leftLine {
  width: 6px;
  height: 20px;
  background: #236dfe;
  border-radius: 4px;
  margin: auto 8px auto 0;
}

.title {
  border-bottom: 1px solid #ccc;
  padding-bottom: 8px;
}

.card {
  margin-bottom: 20px;
}

.content {
  width: 90%;
  position: relative;
  left: 5%;

  p {
    font-size: 14px;
    margin-top: 18px;
  }

  table tr td {
    border: 1px solid #000;
    font-size: 12px;
    text-align: center;
    padding: 10px;
  }
}
</style>
